package pe.edu.upc.dew.Grupo1.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.support.WebApplicationContextUtils;

import pe.edu.upc.dew.Grupo1.model.Cine;
import pe.edu.upc.dew.Grupo1.model.Local;
import pe.edu.upc.dew.Grupo1.model.Tarifa;
import pe.edu.upc.dew.Grupo1.service.CineService;
import pe.edu.upc.dew.Grupo1.service.LocalService;

public class MantLocalServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private CineService cineService;
	private LocalService localService;
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		this.cineService = WebApplicationContextUtils
				.getWebApplicationContext(config.getServletContext()).getBean(
						"cineService", CineService.class);
		
		this.localService = WebApplicationContextUtils
		.getWebApplicationContext(config.getServletContext()).getBean(
				"localService", LocalService.class);

	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		HttpSession session=req.getSession();
		if(session.getAttribute("usuario") == null)
		{
			req.getRequestDispatcher("login.jsp").forward(req,resp);
			return;
		}
		
		String paramCodigoLocal=req.getParameter("codigoLocal");
		
		Cine cine=new Cine();
		cine.setRazonSocial("");
		List<Cine> lista=cineService.GetCinesPorRazonSocial(cine);
	
		req.setAttribute("cines", lista);
		
		if(paramCodigoLocal!= null && !paramCodigoLocal.equals(""))
		{
			int codigoLocal=Integer.parseInt(paramCodigoLocal);
			
			Local localParam=new Local();
			localParam.setCodigoLocal(codigoLocal);
			
			Local local = localService.GetLocalPorCodigo(localParam);
			
			req.setAttribute("local", local);
		}
		
		req.getRequestDispatcher("mant_loc.jsp").forward(req,resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		HttpSession session=req.getSession();
		if(session.getAttribute("usuario") == null)
		{
			req.getRequestDispatcher("login.jsp").forward(req,resp);	
			return;
		}
	
		
		try {
			
			String codigoLocal= req.getParameter("codigo");
			String nombre= req.getParameter("nombre");
			String direccion=req.getParameter("direccion");
			String telefono=req.getParameter("telefono");
			int aforo = Integer.parseInt(req.getParameter("aforo"));
			
			int codigoCine = Integer.parseInt(req.getParameter("cine"));
			double precio=Double.parseDouble(req.getParameter("precio"));	
			
			Cine cine=new Cine();
			cine.setCodigoCine(codigoCine);

			Local local=new Local();
			
			local.setAforo(aforo);
			
			local.setDireccion(direccion);
			local.setNombre(nombre);
			local.setTelefono(telefono);	
			local.setPrecio(precio);		
			
			local.setCine(cine);
			
			int codigo=0;
			
			if(codigoLocal== null || codigoLocal.equals(""))
			{
				codigo= localService.GetConteoLocales()+1;
				local.setCodigoLocal(codigo);
				
				localService.InsertLocal(local);
			}
			else{
				codigo=Integer.parseInt(codigoLocal);
				local.setCodigoLocal(codigo);
				
				localService.UpdateLocalPorCodigo(local);
			}
			
			req.getRequestDispatcher("ListasCartelera").forward(req,resp);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			req.getRequestDispatcher("error.jsp").forward(req,resp);
		}
				
	}
}
